package aer.SerieDePotencias;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SerieDePotencias {
	private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	private static final int modulo = 1000007;

	public static void main(String[] args) throws Exception {

		try {
			programa();
		} catch (Exception e) {

		}

	}

	private static void programa() throws Exception {

		String[] datos = br.readLine().split(" ");

		while (true) {
			// PROGRAMA
			int x = Integer.parseInt(datos[0]);
			int n = Integer.parseInt(datos[1]);
			int acc = 1;
			int factorial = 1;

			for (int i = 1; i <= n; i++) {
				factorial *= x;
				factorial %= modulo;
				acc += factorial;
				acc %= modulo;
			}

			System.out.print(acc + "\n");

			// PROGRAMA
			datos = br.readLine().split(" ");
		}
	}

}